<?php
namespace App\Services\ThirdPayments\Recharge;

use App\Exceptions\RechargeGatewayException;
use App\Lib\Code;
use App\Lib\PaymentMap;
use App\Lib\ThirdPartyMap;
use App\Models\RechargeOrder;
use App\Services\ThirdPayments\Contracts\Actions\HrefRedirectAction;
use App\Services\ThirdPayments\Contracts\Actions\QrCodeAction;
use App\Services\ThirdPayments\Contracts\QRCapable;
use App\Services\ThirdPayments\Contracts\RechargeBaseAbstract;
use App\Services\ThirdPayments\Contracts\WAPable;

class VSZFPayment extends RechargeBaseAbstract implements WAPable, QRCapable
{
    protected $gw_pay = 'http://www.vszhifu.cc/Pay/';

    public function callback(array $data)
    {
        // TODO: Implement callback() method.
        if ($this->veryCallbackSign($data)) {
            if ($data['fxstatus'] == 1) {
                return true;
            }
        }
        return false;
    }

    public function veryCallbackSign(array $data)
    {
        // TODO: Implement veryCallbackSign() method.
        $parseSign = $data['fxstatus'];
        $parseSign .= $data['fxid'];
        $parseSign .= $data['fxddh'];
        $parseSign .= $data['fxfee'];
        $parseSign .= $this->getMchKey();
        if ($data['fxsign'] == md5($parseSign)) {
            return true;
        }
        return false;
    }

    public function getOrderNoFromCallback(array $data)
    {
        // TODO: Implement getOrderNoFromCallback() method.
        return $data['fxddh'];
    }

    public function getThirdNoFromCallback(array $data)
    {
        return $data['fxorder'];
    }

    public function getOrderAmtFromCallback(array $data)
    {
        // TODO: Implement getOrderAmtFromCallback() method.
        return $data['fxfee'];
    }

    /**
     * @return mixed
     */
    public function getIdentify(): string
    {
        // TODO: Implement getIdentify() method.
        return ThirdPartyMap::DAPAI;
    }

    /**
     *  needs child to achieve
     */
    public function initPaymentMap(): array
    {
        // TODO: Implement initPaymentMap() method.
        return [
            PaymentMap::WX => 'wxsm',
            PaymentMap::ALI => 'zfbsm',
            PaymentMap::WX_WAP => 'wxwap',
            PaymentMap::ALI_WAP => 'zfbwap',
        ];
    }

    public function query(RechargeOrder $rechargeOrder)
    {
        $this->setGwQuery($this->gw_pay);
        // TODO: Implement query() method.
        $this->setParameter('fxid', $this->getMchId());
        $this->setParameter('fxddh', $rechargeOrder->plat_no);
        $this->setParameter('fxaction', 'orderquery');
        $this->setParameter('fxsign', $this->querySign());
        $res = curlHttp($this->getGwQuery(), $this->getParameters());
        try {
            if ($this->curlResVerify($this, $res)) {
                $resArr = json_decode($res['body'], true);
                $resSign = $resArr['fxstatus'] .
                    $resArr['fxid'] .
                    $resArr['fxddh'] .
                    $resArr['fxfee'] .
                    $this->getMchKey();
                if ($resArr['fxsign'] == md5($resSign)) {
                    if ($resArr['fxstatus'] == 1) {
                        return true;
                    }
                    return $res['body'];
                }
                return "返回sign验签错误-->resBody:" .  $res['body'];
            }
        } catch (\Exception $exception) {
            return $exception->getMessage();
        }
    }

    public function bankHref(RechargeOrder $rechargeOrder)
    {
        // TODO: Implement bankHref() method.
    }

    public function scanCode(RechargeOrder $rechargeOrder)
    {
        // TODO: Implement scanCode() method.
        $this->initParameters($rechargeOrder);
        $res = curlHttp($this->getGwPay(), $this->getParameters());
        if ($this->curlResVerify($this, $res)) {
            $resArr = json_decode($res['body'], true);
            if ($resArr['status'] == 1) {
                return $resArr['payurl'];
            }
            throw new RechargeGatewayException($this, $res['body'], '接口请求错误【'.$resArr['error'].'】', Code::RECHARGE_THIRD_LOG);
        }
    }

    public function paySign(): string
    {
        // TODO: Implement paySign() method.
        $signStr = $this->getParameter('fxid');
        $signStr .= $this->getParameter('fxddh');
        $signStr .= $this->getParameter('fxfee');
        $signStr .= $this->getParameter('fxnotifyurl');
        $signStr .= $this->getMchKey();
        return md5($signStr);
    }

    public function querySign(): string
    {
        // TODO: Implement querySign() method.
        $signStr = $this->getParameter('fxid');
        $signStr .= $this->getParameter('fxddh');
        $signStr .= $this->getParameter('fxaction');
        $signStr .= $this->getMchKey();
        return md5($signStr);
    }

    public function showSuccess(): string
    {
        // TODO: Implement showSuccess() method.
        return 'success';
    }

    public function initParameters(RechargeOrder $rechargeOrder)
    {
        $mchData = $rechargeOrder->order_data;
        // TODO: Implement initParameters() method.
        $this->setParameter('fxid', $this->getMchId());
        $this->setParameter('fxddh', $rechargeOrder->plat_no);
        $this->setParameter('fxdesc', array_key_exists('body', $mchData) ? $mchData['body'] : '艾尚支付充值');
        $this->setParameter('fxfee', $rechargeOrder->order_amt);
        $this->setParameter('fxattch', $this->setRemark($rechargeOrder));
        $this->setParameter('fxnotifyurl', $this->getCallbackUrl());
        $this->setParameter('fxbackurl', $this->getReturnUrl());
        $this->setParameter('fxpay', $this->getPaymentMap($mchData['recharge_type']));
        $this->setParameter('fxip', request()->getClientIp());
        $this->setParameter('fxsign', $this->paySign());
    }

    public function wapReq(RechargeOrder $rechargeOrder)
    {
        // TODO: Implement wapReq() method.
        $this->initParameters($rechargeOrder);
        $res = curlHttp($this->getGwPay(), $this->getParameters());
        if ($this->curlResVerify($this, $res)) {
            $rechargeOrder->third_return = $res['body'];
            $rechargeOrder->save();
            $resArr = json_decode($res['body'], true);
            if ($resArr['status'] == 1) {
                return new HrefRedirectAction($resArr['payurl']);
            }
            throw new RechargeGatewayException($this, $res['body'], '接口请求错误【'.$resArr['error'].'】', Code::RECHARGE_THIRD_LOG);
        }
    }

    public function qrCode(RechargeOrder $rechargeOrder)
    {
        // TODO: Implement qrCode() method.
        $this->initParameters($rechargeOrder);
        $res = curlHttp($this->getGwPay(), $this->getParameters());
        if ($this->curlResVerify($this, $res)) {
            $rechargeOrder->third_return = $res['body'];
            $rechargeOrder->save();
            $resArr = json_decode($res['body'], true);
            if ($resArr['status'] == 1) {
                $payUrl = $resArr['payurl'];
                parse_str(parse_url($payUrl, PHP_URL_QUERY), $output);
                return new QrCodeAction($output['url']);
            }
            throw new RechargeGatewayException($this, $res['body'], '接口请求错误【'.$resArr['error'].'】', Code::RECHARGE_THIRD_LOG);
        }
    }
}
